package com.cliff.joinus.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.androidquery.AQuery;
import com.cliff.joinus.Constants;
import com.cliff.joinus.JoinusControlPoint;
import com.cliff.joinus.LocalMediaDataManager;
import com.cliff.joinus.R;
import com.cliff.joinus.RendererMediator;
import com.cliff.joinus.Constants.ITEMNODE_TYPE;
import com.cliff.joinus.LocalMediaDataManager.FILE_TYPE;
import com.cliff.joinus.LocalMediaDataManager.MEDIA_TYPE;
import com.cliff.joinus.RendererMediator.MEDIATOR_OPERATION;
import com.cliff.joinus.dataobject.MusicItem;

public class BrowseMusicActivity extends SherlockListActivity {

	protected final int[][] ACTIONBARMENU = new int[][]{Constants.AB_BACK, Constants.AB_SELECT_RENDERER};
	protected ArrayAdapter<MusicItem> mAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setTitle(String.format(getString(R.string.title_activity_browsemusic), LocalMediaDataManager.getInstance().getMusicList().size()));
		initListView();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	protected void initListView() {
		ArrayList<MusicItem> list = LocalMediaDataManager.getInstance().getMusicList();

		mAdapter = new ArrayAdapter<MusicItem>(this,
				R.layout.item_line_music_track, list) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {

				if (convertView == null) {
					LayoutInflater inflater = (LayoutInflater) getApplicationContext()
							.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					convertView = inflater.inflate(
							R.layout.item_line_music_track, null);
				} 

				MusicItem data = getItem(position);
				AQuery listItemAq = new AQuery(convertView);
				listItemAq.id(R.id.trackname).text(data.getDisplayName());
				listItemAq.id(R.id.trackartist).text(data.getArtist());
				listItemAq.id(R.id.tracktime).text(data.getDuration());
				//TODO: add set view here
				return convertView;
			}
		};

		setListAdapter(mAdapter);		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
			
		MainActivity.initMenu(menu, ACTIONBARMENU);

		// return super.onCreateOptionsMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (android.R.id.home == item.getItemId()) {
			finish();
			return true;
		}
		
		if (Constants.GROUP_COMMON == item.getGroupId()) {
			switch(item.getItemId()) {
			case Constants.ITEM_BACK:
				finish();
				break;
			case Constants.ITEM_SELECT_RENDERER:
				break;
			}
		}

//		return super.onOptionsItemSelected(item);
		return true;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		MusicItem item = mAdapter.getItem(position);
		if (null == JoinusControlPoint.getInstance().getSelectedRendererUdn()) {
			//local play
			LocalMediaDataManager.getInstance().playExternal(item.filePathName, MEDIA_TYPE.AUDIO, FILE_TYPE.LOCAL_FILE);
		} 
		else {
			RendererMediator mediator = JoinusControlPoint.getInstance().getSelectedRendererMediator();
			if (null == mediator) {
				Toast.makeText(this, "Mediator is null, renderer not ready", Toast.LENGTH_SHORT).show();
				return;
			}
			mediator.init(ITEMNODE_TYPE.LOCAL_AUDIO, position, null);
			mediator.operate(MEDIATOR_OPERATION.PLAYPAUSE, position);
			
			Intent intent = new Intent(this, PlayControlActivity.class);
			startActivity(intent);			
		}
	}
	
	
}
